package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.Response;
import com.atguigu.eduorder.service.PayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author zhaolie
 * @since 2020-12-20
 */
@RestController
@RequestMapping("/eduorder/paylog")
@Api(description = "支付前台")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @GetMapping("createNative/{orderNo}")
    @ApiOperation("生成微信支付二维码")
    public Response createNative(@PathVariable("orderNo") String orderNo) {
        //返回信息，包含二维码地址，还有其他需要的信息
        Map map = payLogService.createNatvie(orderNo);
        System.out.println("****返回二维码map集合:" + map);
        return Response.SUCCESS().data(map);
    }

    @GetMapping("queryPayStatus/{orderNo}")
    @ApiOperation("根据订单号查询支付状态")
    public Response queryPayStatus(@PathVariable("orderNo") String orderNo) {
        Map map = payLogService.queryPayStatus(orderNo);
        if (map == null) {
            return Response.Failed().message("支付出错了!");
        }
        System.out.println(map);
        // 如果返回的map里面不为空,通过map获取订单状态
        if (map.get("trade_state").equals("SUCCESS")) {
            // 添加记录到支付表,更新订单表状态
            payLogService.updateOrderStatus(map);
            return Response.SUCCESS().message("支付成功");
        }
        System.out.println("****返回支付map集合:" + map);
        return Response.SUCCESS().message("支付中...");
    }
}


